/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import controller.service.*;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTabbedPane;
import javax.swing.table.DefaultTableModel;
import model.dao.ConnectionFactory;
import model.entities.User;
import controller.UserController;
import java.awt.Frame;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import util.Constant;
import util.Excel;

/**
 *
 * @author K u Ku
 */
public class pnlUser extends javax.swing.JPanel {
    
    UserControllerService userServiceCtrl = UserController.getInstance();
    List<User> userList = userServiceCtrl.getUsersList();
    /**
     * Creates new form pnlBase
     */
    public pnlUser() {
        initComponents();
    }
    public pnlUser(JTabbedPane tab) {
        initComponents();
        this.tab = tab;
        List<User> userListUpdate = userServiceCtrl.getUsersList();
        load(userListUpdate);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlList = new javax.swing.JPanel();
        pnlTable = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblData = new javax.swing.JTable();
        pnlSearch = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtSearch = new javax.swing.JTextField();
        cboSearch = new javax.swing.JComboBox();
        btnExport = new javax.swing.JButton();
        pnlDetails = new javax.swing.JPanel();
        pnlButton = new javax.swing.JPanel();
        btnClear = new javax.swing.JButton();
        btnInsert = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        txtNotes = new javax.swing.JTextField();
        cbIsActived = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        cboRole = new javax.swing.JComboBox();
        txtPassword = new javax.swing.JPasswordField();
        txtEmail = new javax.swing.JTextField();
        cboGender = new javax.swing.JComboBox();
        txtPhone = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtAddress = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtIdentityCard = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        dcBirthday = new com.toedter.calendar.JDateChooser();

        setMinimumSize(new java.awt.Dimension(800, 500));
        setPreferredSize(new java.awt.Dimension(800, 500));
        setLayout(new java.awt.BorderLayout());

        pnlList.setLayout(new java.awt.BorderLayout());

        pnlTable.setLayout(new java.awt.BorderLayout());

        tblData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblData.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDataMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblData);

        pnlTable.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pnlList.add(pnlTable, java.awt.BorderLayout.CENTER);

        pnlSearch.setPreferredSize(new java.awt.Dimension(800, 30));
        pnlSearch.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("Search");
        pnlSearch.add(jLabel1);

        txtSearch.setPreferredSize(new java.awt.Dimension(400, 20));
        txtSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchKeyReleased(evt);
            }
        });
        pnlSearch.add(txtSearch);

        cboSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "By ID", "By Name" }));
        cboSearch.setPreferredSize(new java.awt.Dimension(80, 20));
        pnlSearch.add(cboSearch);

        btnExport.setText("Export");
        btnExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportActionPerformed(evt);
            }
        });
        pnlSearch.add(btnExport);

        pnlList.add(pnlSearch, java.awt.BorderLayout.PAGE_START);

        add(pnlList, java.awt.BorderLayout.CENTER);

        pnlDetails.setPreferredSize(new java.awt.Dimension(800, 250));
        pnlDetails.setLayout(new java.awt.BorderLayout());

        pnlButton.setMinimumSize(new java.awt.Dimension(800, 30));
        pnlButton.setPreferredSize(new java.awt.Dimension(800, 30));
        pnlButton.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        btnClear.setText("Clear");
        btnClear.setPreferredSize(new java.awt.Dimension(80, 23));
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });
        pnlButton.add(btnClear);

        btnInsert.setText("Insert");
        btnInsert.setPreferredSize(new java.awt.Dimension(80, 23));
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });
        pnlButton.add(btnInsert);

        btnUpdate.setText("Update");
        btnUpdate.setPreferredSize(new java.awt.Dimension(80, 23));
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        pnlButton.add(btnUpdate);

        btnDelete.setText("Delete");
        btnDelete.setPreferredSize(new java.awt.Dimension(80, 23));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        pnlButton.add(btnDelete);

        btnClose.setText("Close");
        btnClose.setPreferredSize(new java.awt.Dimension(80, 23));
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });
        pnlButton.add(btnClose);

        pnlDetails.add(pnlButton, java.awt.BorderLayout.CENTER);

        jPanel2.setPreferredSize(new java.awt.Dimension(800, 220));

        jLabel12.setText("Notes");

        cbIsActived.setSelected(true);
        cbIsActived.setText("Is Actived");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Account"));

        jLabel14.setText("Role");

        jLabel4.setText("Password");

        jLabel3.setText("User Name");

        cboRole.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "User", "Admin" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtUserName))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel14))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboRole, 0, 153, Short.MAX_VALUE)
                            .addComponent(txtPassword))))
                .addGap(0, 10, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(cboRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30))
        );

        cboGender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female", " " }));

        jLabel10.setText("Email");

        jLabel11.setText("Address");

        jLabel8.setText("Birthday");

        jLabel7.setText("Gender");

        jLabel9.setText("Phone");

        jLabel5.setText("Full Name");

        jLabel6.setText("Identity Card");

        jLabel2.setText("ID");

        txtID.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addGap(42, 42, 42)
                        .addComponent(txtNotes))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addGap(31, 31, 31)
                        .addComponent(txtAddress))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(24, 24, 24))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtIdentityCard, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                            .addComponent(txtID)
                            .addComponent(txtName)
                            .addComponent(dcBirthday, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel7)
                            .addComponent(jLabel10))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addComponent(cbIsActived, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cboGender, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtEmail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5)
                                .addGap(16, 16, 16)
                                .addComponent(jLabel8))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel10))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(cboGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(10, 10, 10)
                                .addComponent(cbIsActived))
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel9)
                                .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dcBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtIdentityCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))))
                .addGap(4, 4, 4)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(7, 7, 7)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(txtNotes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pnlDetails.add(jPanel2, java.awt.BorderLayout.PAGE_START);

        add(pnlDetails, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        tab.remove(this);
    }//GEN-LAST:event_btnCloseActionPerformed

    private void tblDataMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDataMouseClicked
        binding();
    }//GEN-LAST:event_tblDataMouseClicked

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        clear();
    }//GEN-LAST:event_btnClearActionPerformed

    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
        insert();
        userList = userServiceCtrl.getUsersList();
        load(userList);
    }//GEN-LAST:event_btnInsertActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        update();
        userList = userServiceCtrl.getUsersList();
        load(userList);
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        delete();
        userList = userServiceCtrl.getUsersList();
        load(userList);
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchKeyReleased
        String value = txtSearch.getText();
        if(cboSearch.getSelectedIndex() == 0)
        {
            List<User> userList = userServiceCtrl.findUserByID(Integer.parseInt(value));
            load(userList);
        }
        if(cboSearch.getSelectedIndex() == 1){
            List<User> userList = userServiceCtrl.findUserByName(value);
            load(userList);
        }
    }//GEN-LAST:event_txtSearchKeyReleased

    private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportActionPerformed
        export();
    }//GEN-LAST:event_btnExportActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnExport;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JCheckBox cbIsActived;
    private javax.swing.JComboBox cboGender;
    private javax.swing.JComboBox cboRole;
    private javax.swing.JComboBox cboSearch;
    private com.toedter.calendar.JDateChooser dcBirthday;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlButton;
    private javax.swing.JPanel pnlDetails;
    private javax.swing.JPanel pnlList;
    private javax.swing.JPanel pnlSearch;
    private javax.swing.JPanel pnlTable;
    private javax.swing.JTable tblData;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtIdentityCard;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtNotes;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtSearch;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
    JTabbedPane tab = new JTabbedPane();

    //LOAD DATA
    private void load(List<User> userList){
        DefaultTableModel dm = (DefaultTableModel) tblData.getModel();
        String[] columnName = {"ID","Name","Identity Card","Gender","Birthday","Phone",
                                "Email","Address","User Name","Password","Role","Notes"};
        dm.setColumnIdentifiers(columnName);
        dm.setNumRows(0);
        for(User u : userList){
            String[] cell = new String[12];
            cell[0] = String.valueOf(u.getUserID());
            cell[1] = u.getName();
            cell[2] = u.getIdentityCard();
            cell[3] = u.isGender()? "Male" : "Female";
            cell[4] = String.valueOf(u.getBirthday());
            cell[5] = u.getPhone();
            cell[6] = u.getEmail();
            cell[7] = u.getAddress();
            cell[8] = u.getUserName();
            cell[9] = u.getPassword();
            cell[10] = u.isRole() ? "Admin" : "User";
            cell[11] = u.getNotes();
            dm.addRow(cell);
        }
    }
    
    //BINDING DATA
    private void binding(){
        int index = tblData.getSelectedRow();
        if(index > -1){
            txtID.setText((String) tblData.getValueAt(index, 0));
            txtName.setText((String) tblData.getValueAt(index, 1));
            txtIdentityCard.setText((String) tblData.getValueAt(index, 2));
            cboGender.setSelectedItem(tblData.getValueAt(index, 3));
            if(tblData.getValueAt(index, 4) != null){
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    dcBirthday.setDate(df.parse((String) tblData.getValueAt(index, 4)));

                } catch (ParseException ex) {
                    Logger.getLogger(pnlUser.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            txtPhone.setText((String) tblData.getValueAt(index, 5));
            txtEmail.setText((String) tblData.getValueAt(index, 6));
            txtAddress.setText((String) tblData.getValueAt(index, 7));
            txtUserName.setText((String) tblData.getValueAt(index, 8));
            txtPassword.setText((String) tblData.getValueAt(index, 9));
            cboRole.setSelectedItem(tblData.getValueAt(index, 10));
            txtNotes.setText((String) tblData.getValueAt(index, 11));
        }
    }
    
    //CLEAR DATA IN COMPONENTS
    private void clear(){
        txtName.requestFocus(true);
        txtID.setText("");
        txtAddress.setText("");
        txtEmail.setText("");
        txtIdentityCard.setText("");
        txtName.setText("");
        txtNotes.setText("");
        txtPassword.setText("");
        txtPhone.setText("");
        txtSearch.setText("");
        txtUserName.setText("");
        dcBirthday.setDate(null);
    }
    
    //INSERT
    private void insert(){
        String name = txtName.getText();
        Date utilDate = dcBirthday.getDate();
        java.sql.Date birthday = new java.sql.Date(utilDate.getTime());
        String userName = txtUserName.getText();
        String password = txtPassword.getText();
        boolean role;
        if(cboRole.getSelectedIndex() == 0){
            role = true;
        }
        else{
            role = false;
        }
        String identityCard = txtIdentityCard.getText();
        boolean gender;
        if(cboGender.getSelectedIndex() == 0){
            gender = true;
        }
        else{
            gender = false;
        }
        String phone = txtPhone.getText();
        String email = txtEmail.getText();
        String address = txtAddress.getText();
        String notes = txtNotes.getText();
        User user = new User(1, userName, password, role, name, identityCard, gender, birthday, phone, email, address, notes, true);
        if(userServiceCtrl.insertUser(user)){
            JOptionPane.showMessageDialog(this, "Insert success.");
            Constant.status = "Insert success.";
        }
        else{
            JOptionPane.showMessageDialog(this, "Insert failed.");
            Constant.status = "Insert failed.";
        }
    }
    
    //UPDATE
    private void update(){
        int id = Integer.parseInt(txtID.getText());
        String name = txtName.getText();
        Date utilDate = dcBirthday.getDate();
        java.sql.Date birthday = new java.sql.Date(utilDate.getTime());
        String userName = txtUserName.getText();
        String password = txtPassword.getText();
        boolean role;
        if(cboRole.getSelectedIndex() == 0){
            role = true;
        }
        else{
            role = false;
        }
        String identityCard = txtIdentityCard.getText();
        boolean gender;
        if(cboGender.getSelectedIndex() == 0){
            gender = true;
        }
        else{
            gender = false;
        }
        String phone = txtPhone.getText();
        String email = txtEmail.getText();
        String address = txtAddress.getText();
        String notes = txtNotes.getText();
        User user = new User(id, userName, password, role, name, identityCard, gender, birthday, phone, email, address, notes, true);
        if(userServiceCtrl.updateUser(user)){
            JOptionPane.showMessageDialog(this, "Update success.");
        }
        else{
            JOptionPane.showMessageDialog(this, "Update failed.");
        }
    }
    
    //DELETE
    private void delete(){
        int id = Integer.parseInt(txtID.getText());
        if(id <0){
            JOptionPane.showMessageDialog(this, "You must choose User ID to delete.");
            return;
        }
        else{
            int choose = JOptionPane.showConfirmDialog(this, "Do you want to delete user \""+txtUserName.getText()+"\" ?", "Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(choose == 0){
                if(userServiceCtrl.deleteUser(id)){
                    JOptionPane.showMessageDialog(this, "Delete success.");
                }
                else{
                    JOptionPane.showMessageDialog(this, "Delete failed.");
                }
            }
        }
    }
    
    //EXPORT
    private void export(){
        try {
            String [][] value = new String[tblData.getRowCount()][tblData.getColumnCount()];
            for (int i = 0; i < tblData.getRowCount(); i++) {
                for (int j = 0; j < tblData.getColumnCount(); j++) {
                    value[i][j] = (String) tblData.getValueAt(i, j);
                }
            }
            Excel.exportData("UserList.xls", "User", value);
            JOptionPane.showMessageDialog(this, "Export data success.");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Export data failed.");
            Logger.getLogger(pnlUser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

